home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / lfs / lfsIo.c < prev    next >
C/C++ Source or Header  |  1991-07-26  |  5KB  |  179 lines

  1. /* 
  2.  * lfsSegIo.c --
  3.  *
  4.  *    Read and write bytes from LFS segments.
  5.  *
  6.  * Copyright 1989 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/kernel/lfs/RCS/lfsIo.c,v 1.7 91/07/26 17:18:48 mendel Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <sprite.h>
  21. #include <lfs.h>
  22. #include <lfsInt.h>
  23. #include <dev.h>
  24. #include <fs.h>
  25. #include <devFsOpTable.h>
  26.  
  27.  
  28. /*
  29.  *----------------------------------------------------------------------
  30.  *
  31.  * LfsReadBytes --
  32.  *
  33.  *    Read bytes from an lfs disk.
  34.  *
  35.  * Results:
  36.  *    Error status.
  37.  *
  38.  * Side effects:
  39.  *    None.
  40.  *
  41.  *----------------------------------------------------------------------
  42.  */
  43.  
  44. ReturnStatus
  45. LfsReadBytes(lfsPtr, diskAddress, numBytes, bufferPtr)
  46.     Lfs        *lfsPtr;    /* Target file system. */
  47.     LfsDiskAddr diskAddress;    /* Disk address to read from. */
  48.     int        numBytes;    /* Number of bytes to read. */
  49.     char    *bufferPtr;    /* Buffer to read into. */
  50. {
  51.     struct args { 
  52.     Fs_IOParam    readParams;
  53.     Fs_IOReply    reply;
  54.     } args;
  55.     ReturnStatus status;
  56.     char    smallBuffer[DEV_BYTES_PER_SECTOR];
  57.     int    offset;
  58.  
  59.     /*
  60.      * Check in the seg cache.
  61.      */
  62.     if (lfsPtr->segCache.valid) {
  63.     if (LfsDiskAddrInRange(diskAddress, LfsBytesToBlocks(lfsPtr, numBytes),
  64.                 lfsPtr->segCache.startDiskAddress,
  65.                 lfsPtr->segCache.endDiskAddress)) {
  66.         offset = LfsDiskAddrOffset(diskAddress, 
  67.             lfsPtr->segCache.startDiskAddress);
  68.         offset = LfsBlocksToBytes(lfsPtr, offset);
  69.         bcopy(lfsPtr->segCache.memPtr + offset, bufferPtr, numBytes);
  70.         LFS_STATS_INC(lfsPtr->stats.blockio.segCacheHits);
  71.         return SUCCESS;
  72.     }
  73.     }
  74.  
  75.  
  76.     bzero((char *)&args, sizeof(args));
  77.  
  78.     if (numBytes < DEV_BYTES_PER_SECTOR) { 
  79.     args.readParams.buffer = smallBuffer;
  80.     args.readParams.length = DEV_BYTES_PER_SECTOR;
  81.     } else {
  82.     args.readParams.buffer = bufferPtr;
  83.     args.readParams.length = numBytes;
  84.     }
  85.     offset = LfsDiskAddrToOffset(diskAddress);
  86.     args.readParams.offset = offset * DEV_BYTES_PER_SECTOR;
  87.     args.readParams.flags = 
  88.     args.readParams.procID = 0;
  89.     args.readParams.familyID = 0;
  90.     args.readParams.uid = 0;
  91.     args.readParams.reserved = 0;
  92.     status = (*devFsOpTable[DEV_TYPE_INDEX(lfsPtr->devicePtr->type)].read)
  93.         (lfsPtr->devicePtr, &args.readParams, &args.reply);
  94.     if (status != SUCCESS) {
  95.     LfsError(lfsPtr, status, "LfsReadBytes failed");
  96.     }
  97.     if (numBytes < DEV_BYTES_PER_SECTOR) {
  98.     if (args.reply.length != DEV_BYTES_PER_SECTOR) {
  99.         LfsError(lfsPtr, FAILURE, "LfsReadBytes short read");
  100.     }
  101.     bcopy(smallBuffer, bufferPtr, numBytes);
  102.     } else {
  103.     if (args.reply.length != numBytes) {
  104.         LfsError(lfsPtr, FAILURE, "LfsReadBytes short read");
  105.     }
  106.     }
  107.     LFS_STATS_ADD(lfsPtr->stats.blockio.totalBytesRead, numBytes);
  108.     return status;
  109. }
  110.  
  111. /*
  112.  *----------------------------------------------------------------------
  113.  *
  114.  * LfsWriteBytes --
  115.  *
  116.  *    Write bytes from an lfs disk.
  117.  *
  118.  * Results:
  119.  *    Error status.
  120.  *
  121.  * Side effects:
  122.  *    None.
  123.  *
  124.  *----------------------------------------------------------------------
  125.  */
  126.  
  127. ReturnStatus
  128. LfsWriteBytes(lfsPtr, diskAddress, numBytes, bufferPtr)
  129.     Lfs        *lfsPtr;    /* Target file system. */
  130.     LfsDiskAddr diskAddress;    /* Disk address to send data. */
  131.     int        numBytes;    /* Number of bytes to write. */
  132.     char    *bufferPtr;    /* Buffer to write into. */
  133. {
  134.     struct args { 
  135.     Fs_IOParam    writeParams;
  136.     Fs_IOReply    reply;
  137.     } args;
  138.     char    smallBuffer[DEV_BYTES_PER_SECTOR];
  139.     ReturnStatus    status;
  140.     int    offset;
  141.  
  142.  
  143.     offset = LfsDiskAddrToOffset(diskAddress);
  144.     bzero((char *)&args, sizeof(args));
  145.  
  146.     if (numBytes < DEV_BYTES_PER_SECTOR) { 
  147.     args.writeParams.buffer = smallBuffer;
  148.     args.writeParams.length = DEV_BYTES_PER_SECTOR;
  149.     bzero(smallBuffer + numBytes, DEV_BYTES_PER_SECTOR-numBytes);
  150.     bcopy(bufferPtr, smallBuffer, numBytes);
  151.     } else {
  152.     args.writeParams.buffer = bufferPtr;
  153.     args.writeParams.length = numBytes;
  154.     }
  155.     args.writeParams.offset = offset * DEV_BYTES_PER_SECTOR;
  156.     args.writeParams.flags = 
  157.     args.writeParams.procID = 0;
  158.     args.writeParams.familyID = 0;
  159.     args.writeParams.uid = 0;
  160.     args.writeParams.reserved = 0;
  161.     status = (*devFsOpTable[DEV_TYPE_INDEX(lfsPtr->devicePtr->type)].write)
  162.         (lfsPtr->devicePtr, &args.writeParams, &args.reply);
  163.     if (status != SUCCESS) {
  164.     LfsError(lfsPtr, status, "LfsWriteBytes");
  165.     }
  166.     if (numBytes < DEV_BYTES_PER_SECTOR) {
  167.     if (args.reply.length != DEV_BYTES_PER_SECTOR) {
  168.         LfsError(lfsPtr, FAILURE, "LfsWriteBytes short write");
  169.     }
  170.     } else {
  171.     if (args.reply.length != numBytes) {
  172.         LfsError(lfsPtr, FAILURE, "LfsWriteBytes short write");
  173.     }
  174.     }
  175.     LFS_STATS_ADD(lfsPtr->stats.blockio.totalBytesWritten, numBytes);
  176.     return status;
  177. }
  178.  
  179.